<template>
    <div class="sort">
        <div class="container">
            <div class="tap">
                <h5>订单</h5>
                <van-icon name="search" />
            </div>
            <div v-for="(item, index) in items" :key="item.id" class="box">
                <div class="img" :style="{ backgroundImage: `url(${item.foods[1].img})` }"></div>
                <div class="text">
                    <span>{{ item.foods[1].name }}</span>  <!-- 修改为动态渲染 -->
                    <div class="main">
                        <span>月售:</span>
                        <span>{{ item.foods[1].sales }}</span>
                    </div>
                    <div class="rich">
                        <span>￥{{ item.foods[1].price.toFixed(2) }}</span>  <!-- 格式化价格为2位小数 -->
                    </div>
                </div>
                <div class="ok">
                    <span>已支付</span>
                </div>
                <div class="delete-button" @click.prevent="deleteItem(index)">
                    删除
                </div>
            </div>
            <p v-if="items.length === 0">没有更多订单了~</p>
        </div>
    </div>
</template>

<style lang="less" scoped>
.tap {
    display: flex;
    justify-content: space-between;
}
h5 {
    font-size: 16px;
    margin-top: 10px;
}
.van-icon {
    font-size: 20px;
    margin-top: 10px;
}
.box {
    width: 100%;
    height: 108px;
    margin-top: 20px;
    display: flex;
    background-color: #ffffff;
    border-radius: 16px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    position: relative; /* 为了放置删除按钮 */
    .text {
        margin-top: 10px;
        margin-left: 15px;
        span {
            font-size: 16px;
            font-weight: bold;
        }
        .main span {
            font-size: 12px;
            margin-right: 5px;
            color: #c4c4c4;
        }
        .rich span {
            font-size: 14px;
            color: red;
        }
    }
    .ok {
        padding-left: 90px;
        padding-top: 10px;
        span {
            font-size: 14px;
            color: #1EC28A;
        }
    }
    .img {
        width: 108px;
        height: 108px;
        border-radius: 16px;
        background-size: cover;
        background-position: center;
    }
}
.delete-button {
    width: 60px;
    height: 30px;
    font-size: 14px;
    text-align: center;
    position: absolute;
    bottom: 10px;
    right: 10px;
    background-color: transparent;
    border: 1px solid #c4c4c4;
    color: #000;
    padding: 5px 10px;
    border-radius: 5px;
    cursor: pointer;
}
p {
    width: 100%;
    text-align: center;
    font-size: 14px;
    color: #c4c4c4;
    margin: 20px auto;
}
</style>

<script>
import { Dialog } from 'vant';

export default {
    data() {
        return {
            items: this.initializeItems()
        }
    },
    methods: {
        initializeItems() {
            try {
                return [
                    { id: 2, name: '煲仔饭', active: false, foods: [
                        { name: '三宝煲仔饭', sales: 500, price: 25.99, img: 'https://img0.baidu.com/it/u=1511088578,1076721256&fm=253&fmt=auto&app=138&f=JPEG?w=888&h=500' },
                        { name: '顺德煲仔饭', sales: 200, price: 22.99, img: 'https://img0.baidu.com/it/u=735706756,3845746614&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500' },
                    ] },
                    // 其他食品...
                ];
            } catch (error) {
                console.error("初始化数据时发生错误:", error);
                return []; // 返回空数组以避免后续渲染出错
            }
        },
        deleteItem(index) {
            if (index >= 0 && index < this.items.length) {
                Dialog.confirm({
                    title: '提示',
                    message: '确定要删除这项吗？',
                    confirmButtonColor: '#1EC28A'
                })
                .then(() => {
                    this.items.splice(index, 1); // 删除选中的项
                })
                .catch(() => {
                    // 取消操作
                });
            } else {
                console.error('无效的索引:', index);
            }
        }
    },
}

</script>
